
##########################
### General libraries ####
##########################


import pandas as pd
import re
import spacy
import numpy as np
import fasttext
import pickle
import requests
import json
import time
import os


######################
### Own functions ####
######################

def distance_calculation(single_vector, x):    
    try:
        result = np.array(x.tolist())@single_vector.reshape(100,1) #change if needed for dimensions trained in model
        result = result[0]
    except:
        result = np.nan
    return(result)

def vector_normalize(vector):
    norm = np.linalg.norm(vector)
    result = vector/norm
    return(result)


def extract_from_perspective_api(content,index):        
    
    responses = []
    payload = []
    number_random = []
    
    time.sleep(1) #to ensure rate limit is not problematic 
   
    
    if len(content)> 0: 
        try:
                 
            data_dict = {
                'comment': {'text': content},
                'languages': ['en'],
                'requestedAttributes': {'TOXICITY': {}}
            }
           
            response = requests.post(url=url, data=json.dumps(data_dict)) 
            response_dict = json.loads(response.content) 
            responses.append(json.dumps(response_dict, indent=2))
            payload.append(content)
            
        except:
            responses.append("no response")
    else:
        pass
    
    return(responses)

mentionFinder = re.compile(r"@[a-z0-9_]{1,15}", re.IGNORECASE)

def example_tweet_plot(file = "horse"):   
 
    r_script = """
    
    library(ggplot2)
    library(readr)
    library(viridis)
    
    data = read_csv("graph_examples.csv")
    
   

    data$`Toxicity score` = data$toxicity 


    plot_to_save = ggplot(data, aes(x = political, y = hate)) +
      geom_point(aes(size = `Toxicity score`))  +
      geom_text(aes(label=id),hjust=0, vjust=2)  +
      scale_colour_viridis(option = "magma", direction = -1) +
      xlab("Cosine similarity distance from political word vector") +
      ylab("Cosine similarity distance from hate word vector") +
      theme_classic() +
      theme(text = element_text(size=15)) +  
      theme(legend.position = "bottom") 

    ggsave("example_tweets_plot.jpg", plot = plot_to_save, width = 10, height = 10)

    """ 

    with open("temp.R", "w") as f:
        f.write(r_script)
    f.close()

    os.system("Rscript temp.R")

#################
###  Models  ####
#################


#load spacy model
nlp = spacy.load("en_core_web_lg")

#load fasttext model 
model = fasttext.load_model("model_facebook.bin")


